package cn.zysuyuan.user.controller;

import cn.zysuyuan.common.entity.PageResult;
import cn.zysuyuan.common.entity.R;
import cn.zysuyuan.user.entity.User;
import cn.zysuyuan.user.entity.UserUpdate;
import cn.zysuyuan.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * @author flypig
 * @version 1.0
 * @date 2019-11-25 16:36
 */

@Api(tags = "用户账号管理")
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "用户注册", notes = "用户注册")
    @PostMapping("register")
    public R register(@Valid User user) {

        this.userService.register(user);
        return R.ok("成功注册用户");
    }

    @ApiOperation(value = "查询用户", notes = "查询用户")
    @GetMapping("query")
    public ResponseEntity<User> selectUser(@RequestParam(value = "username", required = false)String uName, @RequestParam(value = "password", required = false)String uPass) {
        User user = this.userService.selectUser(uName,uPass);
        if(user == null) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        return ResponseEntity.ok(user);
    }

    @ApiOperation(value = "删除用户", notes = "删除用户")
    @DeleteMapping("delete")
    public R deleteUser(@RequestParam("id") String id) {
        userService.deleteUser(id);
        return R.ok("删除成功");
    }

    @ApiOperation(value = "更改密码时查询用户", notes = "更改密码时查询用户")
    @PostMapping("queryUser")
    public R queryUser(@RequestParam(value = "username", required = false)String uName, @RequestParam(value = "password", required = false)String uPass) {
        User user = this.userService.selectUser(uName,uPass);
        if(user == null) {
            return R.error();
        }
        Map<String,Object> map = new HashMap<>();
        map.put("data",user);
        return R.ok(map);
    }

    @ApiOperation(value = "更新用户", notes = "更新用户")
    @PutMapping("update")
    public R updateUser(@RequestBody UserUpdate userUpdate) {
        this.userService.updateUser(userUpdate);
        return R.ok("更新用户成功");
    }

    @ApiOperation(value = "查询用户名是否重复", notes = "查询用户名是否重复")
    @GetMapping("queryUserName")
    public R queryUserName(@RequestParam("username") String username) {
        int sum = userService.queryUserName(username);
        return R.ok(sum);
    }

    @ApiOperation(value = "查询昵称是否重复", notes = "查询昵称是否重复")
    @GetMapping("queryNickName")
    public R queryNickName(@RequestParam("nickname") String nickname) {
        int sum = userService.queryNickName(nickname);
        return R.ok(sum);
    }

    @ApiOperation(value = "查询全部用户", notes = "查询全部用户")
    @GetMapping("allUser")
    public ResponseEntity<PageResult<User>> queryAllUser(@RequestParam Map<String,Object> map) {
        PageResult<User> result = userService.selectAllUser(map);
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value = "重置密码", notes = "重置密码")
    @PostMapping("resetPassword")
    public R resetPassword(@RequestParam("id") String id) {
        userService.resetPassword(id);
        return R.ok("重置密码成功");
    }

    @ApiOperation(value = "查询用户及其角色信息", notes = "查询用户及其角色信息")
    @GetMapping("roleList")
    public ResponseEntity<User> getRoleList(@RequestParam("id") String id) {
        User user = userService.getRoleList(id);
        return ResponseEntity.ok(user);
    }
}
